 package server.maps;

 import client.MapleCharacter;
 import client.MapleClient;
 import handling.world.WorldFindService;
 import java.awt.Point;
 import java.util.ArrayList;
 import java.util.List;
 import server.MaplePortal;
 import tools.MaplePacketCreator;
 import tools.packet.PartyPacket;





 public class MapleDoor
   extends MapleMapObject
 {
   public final boolean first = true;
   private MapleMap town;
   private MaplePortal townPortal;
   private MapleMap target;
   private int skillId;
   private int ownerId;
   private Point targetPosition;

   public MapleDoor(MapleCharacter owner, Point targetPosition, int skillId) {
/*  29 */     this.ownerId = owner.getId();
/*  30 */     this.target = owner.getMap();
/*  31 */     this.targetPosition = targetPosition;
/*  32 */     setPosition(this.targetPosition);
/*  33 */     this.town = this.target.getReturnMap();
/*  34 */     this.townPortal = getFreePortal();
/*  35 */     this.skillId = skillId;
   }


   public MapleDoor(MapleDoor origDoor) {
/*  40 */     this.town = origDoor.town;
/*  41 */     this.townPortal = origDoor.townPortal;
/*  42 */     this.target = origDoor.target;
/*  43 */     this.targetPosition = new Point(origDoor.targetPosition);
/*  44 */     this.skillId = origDoor.skillId;
/*  45 */     this.ownerId = origDoor.ownerId;
/*  46 */     setPosition(this.townPortal.getPosition());
   }

   public int getSkill() {
/*  50 */     return this.skillId;
   }

   public int getOwnerId() {
/*  54 */     return this.ownerId;
   }

   private MaplePortal getFreePortal() {
/*  58 */     List<MaplePortal> freePortals = new ArrayList<>();

/*  60 */     for (MaplePortal port : this.town.getPortals()) {
/*  61 */       if (port.getType() == 6) {
/*  62 */         freePortals.add(port);
       }
     }
/*  65 */     freePortals.sort((o1, o2) -> (o1.getId() < o2.getId()) ? -1 : ((o1.getId() == o2.getId()) ? 0 : 1));








/*  74 */     for (MapleMapObject obj : this.town.getAllDoorsThreadsafe()) {
/*  75 */       MapleDoor door = (MapleDoor)obj;
/*  76 */       if (door.getOwner() != null && door.getOwner().getParty() != null && getOwner() != null && getOwner().getParty() != null && getOwner().getParty().getPartyId() == door.getOwner().getParty().getPartyId()) {
/*  77 */         return null;
       }
/*  79 */       freePortals.remove(door.getTownPortal());
     }
/*  81 */     if (freePortals.size() <= 0) {
/*  82 */       return null;
     }
/*  84 */     return freePortals.iterator().next();
   }


   public void sendSpawnData(MapleClient client) {
/*  89 */     if (getOwner() == null || this.target == null || client.getPlayer() == null) {
       return;
     }
/*  92 */     if (this.target.getId() == client.getPlayer().getMapId() || getOwnerId() == client.getPlayer().getId() || (getOwner() != null && getOwner().getParty() != null && client.getPlayer().getParty() != null && getOwner().getParty().getPartyId() == client.getPlayer().getParty().getPartyId())) {
/*  93 */       client.announce(MaplePacketCreator.spawnDoor(getOwnerId(), getSkill(), (this.target.getId() == client.getPlayer().getMapId()) ? this.targetPosition : this.townPortal.getPosition(), (this.target.getId() == client.getPlayer().getMapId())));
/*  94 */       if (getOwner() != null && getOwner().getParty() != null && client.getPlayer().getParty() != null && (getOwnerId() == client.getPlayer().getId() || getOwner().getParty().getPartyId() == client.getPlayer().getParty().getPartyId())) {
/*  95 */         client.announce(PartyPacket.partyPortal(this.town.getId(), this.target.getId(), this.skillId, (this.target.getId() == client.getPlayer().getMapId()) ? this.targetPosition : this.townPortal.getPosition(), true));
       }
/*  97 */       client.announce(MaplePacketCreator.spawnPortal(this.town.getId(), this.target.getId(), this.skillId, (this.target.getId() == client.getPlayer().getMapId()) ? this.targetPosition : this.townPortal.getPosition()));
     }
   }


   public void sendDestroyData(MapleClient client) {
/* 103 */     if (client.getPlayer() == null || getOwner() == null || this.target == null) {
       return;
     }
/* 106 */     if (this.target.getId() == client.getPlayer().getMapId() || getOwnerId() == client.getPlayer().getId() || (getOwner() != null && getOwner().getParty() != null && client.getPlayer().getParty() != null && getOwner().getParty().getPartyId() == client.getPlayer().getParty().getPartyId())) {
/* 107 */       client.announce(MaplePacketCreator.removeDoor(getOwnerId(), false));
/* 108 */       if (getOwner() != null && getOwner().getParty() != null && client.getPlayer().getParty() != null && (getOwnerId() == client.getPlayer().getId() || getOwner().getParty().getPartyId() == client.getPlayer().getParty().getPartyId())) {
/* 109 */         client.announce(PartyPacket.partyPortal(999999999, 999999999, 0, new Point(-1, -1), false));
       }
/* 111 */       client.announce(MaplePacketCreator.spawnPortal(999999999, 999999999, 0, null));
     }
   }

   public void warp(MapleCharacter chr, boolean toTown) {
/* 116 */     if (chr.getId() == getOwnerId() || (getOwner() != null && getOwner().getParty() != null && chr.getParty() != null && getOwner().getParty().getPartyId() == chr.getParty().getPartyId())) {
/* 117 */       if (!toTown) {
/* 118 */         chr.changeMap(this.target, this.target.findClosestPortal(this.targetPosition));
       } else {
/* 120 */         chr.changeMap(this.town, this.townPortal);
       }
     } else {
/* 123 */       chr.send(MaplePacketCreator.enableActions());
     }
   }

   public MapleCharacter getOwner() {
/* 128 */     return WorldFindService.getInstance().findCharacterById(this.ownerId);
   }

   public MapleMap getTown() {
/* 132 */     return this.town;
   }

   public MaplePortal getTownPortal() {
/* 136 */     return this.townPortal;
   }

   public MapleMap getTarget() {
/* 140 */     return this.target;
   }

   public Point getTargetPosition() {
/* 144 */     return this.targetPosition;
   }


   public MapleMapObjectType getType() {
/* 149 */     return MapleMapObjectType.DOOR;
   }

   public MapleDoor() {}
 }


/* Location:              C:\PGX\Server.jar!\server\maps\MapleDoor.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */